package com.cypher.encryptionanddecryption.fragment;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.adapter.BluetoothListAdapter;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothGattManager;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothManagerSingleton;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothUtilManager;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class BluetoothInfoListFragment extends Fragment implements AdapterView.OnItemClickListener, BluetoothListAdapter.Callback {
    public ListView bluetoothList;
    private BluetoothUtilManager bluetoothUtilManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bluetooth_list, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bluetoothList = view.findViewById(R.id.bluetooth_list);
        bluetoothUtilManager =  BluetoothManagerSingleton.getInstance().getBluetoothUtilManager();
        BluetoothListAdapter listAdapter = bluetoothUtilManager.getListAdapter();
        if (listAdapter == null) {
            listAdapter = new BluetoothListAdapter(view.getContext());
            listAdapter.setCallback(this);
            listAdapter.setDevices(bluetoothUtilManager.getDisplayDevices());
            bluetoothUtilManager.setListAdapter(listAdapter);
        }
        bluetoothUtilManager.setBluetoothList(bluetoothList);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onClick(View v) {
        BluetoothManagerSingleton bluetoothManagerSingleton = BluetoothManagerSingleton.getInstance();
        // 获取蓝牙连接
        BluetoothGattManager bluetoothGattManager = bluetoothManagerSingleton.getBluetoothGattManager();
        // 第一次进行蓝牙连接
        if(bluetoothGattManager == null) {
            // 初始化蓝牙连接管理类
            // 蓝牙连接管理类存储到单例中，保证在一次会话中只创建一次
            bluetoothManagerSingleton.setBluetoothGattManager(new BluetoothGattManager(v));
        } else {
            Map<String, Object> tag = (Map<String, Object>) v.getTag();
            // 按钮为断开蓝牙状态，关闭当前蓝牙
            if(StringUtils.equals("disconnect", tag.get("state").toString())) {
                bluetoothGattManager.closeGatt(v);
            } else { // 开启蓝牙连接
                bluetoothGattManager.startGatt(v);
            }
        }
    }
}
